package icu.cqcai.xinsi.mapper;

import icu.cqcai.xinsi.domain.InterviewAnalysis;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 面试分析数据访问层接口
 */
@Mapper
public interface InterviewAnalysisMapper {
    
    /**
     * 插入面试分析记录
     */
    @Insert("INSERT INTO interview_analysis (user_id, session_id, technical_score, expression_score, " +
            "logic_score, experience_score, strengths, weaknesses, improvements, next_steps, " +
            "expire_time, create_time, update_time) " +
            "VALUES (#{userId}, #{sessionId}, #{technicalScore}, #{expressionScore}, " +
            "#{logicScore}, #{experienceScore}, #{strengths}, #{weaknesses}, #{improvements}, " +
            "#{nextSteps}, #{expireTime}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertAnalysis(InterviewAnalysis analysis);
    
    /**
     * 根据会话ID查询分析记录
     */
    @Select("SELECT * FROM interview_analysis WHERE session_id = #{sessionId}")
    InterviewAnalysis findBySessionId(@Param("sessionId") String sessionId);
    
    /**
     * 根据用户ID查询分析记录列表
     */
    @Select("SELECT * FROM interview_analysis WHERE user_id = #{userId} ORDER BY create_time DESC")
    java.util.List<InterviewAnalysis> findByUserId(@Param("userId") Long userId);
    
    /**
     * 更新分析记录
     */
    @Update("UPDATE interview_analysis SET technical_score = #{technicalScore}, " +
            "expression_score = #{expressionScore}, logic_score = #{logicScore}, " +
            "experience_score = #{experienceScore}, strengths = #{strengths}, " +
            "weaknesses = #{weaknesses}, improvements = #{improvements}, " +
            "next_steps = #{nextSteps}, report_url = #{reportUrl}, " +
            "update_time = #{updateTime} WHERE id = #{id}")
    int updateAnalysis(InterviewAnalysis analysis);
    
    /**
     * 删除分析记录
     */
    @Delete("DELETE FROM interview_analysis WHERE id = #{id}")
    int deleteAnalysis(@Param("id") Long id);
    
    /**
     * 根据会话ID删除分析记录
     */
    @Delete("DELETE FROM interview_analysis WHERE session_id = #{sessionId}")
    int deleteBySessionId(@Param("sessionId") String sessionId);
    
    /**
     * 检查分析记录是否存在
     */
    @Select("SELECT COUNT(*) FROM interview_analysis WHERE session_id = #{sessionId}")
    int countBySessionId(@Param("sessionId") String sessionId);
    
    /**
     * 清理过期的分析记录
     */
    @Delete("DELETE FROM interview_analysis WHERE expire_time < NOW()")
    int cleanupExpiredAnalysis();
    
    /**
     * 检查分析报告是否存在
     */
    @Select("SELECT COUNT(*) > 0 FROM interview_analysis WHERE session_id = #{sessionId}")
    boolean existsBySessionId(@Param("sessionId") String sessionId);
    
    /**
     * 删除过期的分析报告
     */
    @Delete("DELETE FROM interview_analysis WHERE expire_time < #{currentTime}")
    int deleteExpiredAnalysis(@Param("currentTime") LocalDateTime currentTime);
    
    /**
     * 获取用户的有效分析报告数量
     */
    @Select("SELECT COUNT(*) FROM interview_analysis WHERE user_id = #{userId} AND expire_time > NOW()")
    int getValidAnalysisCount(@Param("userId") Long userId);
    
    /**
     * 获取即将过期的分析报告（7天内）
     */
    @Select("SELECT * FROM interview_analysis WHERE user_id = #{userId} AND expire_time BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY)")
    List<InterviewAnalysis> getExpiringAnalysis(@Param("userId") Long userId);
} 